package com.wetao.trackscreendemo;

import android.Manifest;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    TextView tv_keyDown;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            permission();
        }

        registSreenStatusReceiver();

        findViewById(R.id.btn_RecordAndPlayActivity).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, RecordAndPlayActivity.class));
            }
        });

        findViewById(R.id.btn_Record).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, RecordActivity.class));
            }
        });


        findViewById(R.id.btn_PlayActivity).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, PlayActivity.class));
            }
        });
        findViewById(R.id.btn_RevisePlayActivity).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, RevisePlayActivity.class));
            }
        });

        findViewById(R.id.btn_Camera).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, CameraActivity.class));
            }
        });

        tv_keyDown = findViewById(R.id.tv_keyDown);



    }

    private static final int PERMISSION_REQUEST_CODE = 102;
    private void permission(){
        List<String> permissionLists = new ArrayList<>();
        if(ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){
            permissionLists.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
        }
        if(ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){
            permissionLists.add(Manifest.permission.READ_EXTERNAL_STORAGE);
        }

        if(ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED){
            permissionLists.add(Manifest.permission.RECORD_AUDIO);
        }
        if(ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_NETWORK_STATE) != PackageManager.PERMISSION_GRANTED){
            permissionLists.add(Manifest.permission.ACCESS_NETWORK_STATE);
        }
        if(ContextCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED){
            permissionLists.add(Manifest.permission.READ_PHONE_STATE);
        }


        if(!permissionLists.isEmpty()){//说明肯定有拒绝的权限
            ActivityCompat.requestPermissions(this, permissionLists.toArray(new String[permissionLists.size()]), PERMISSION_REQUEST_CODE);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case PERMISSION_REQUEST_CODE:
                if(grantResults.length > 0){
                    for(int grantResult : grantResults){
                        if(grantResult != PackageManager.PERMISSION_GRANTED){
                            finish();
                            return;
                        }
                    }
                }
                break;
            default:
                break;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        Log.i("key", "按钮" + keyCode);
        if(keyCode == KeyEvent.KEYCODE_PAGE_UP) {
            tv_keyDown.setText("按钮向上：" + keyCode);
        }else if(keyCode == KeyEvent.KEYCODE_PAGE_DOWN) {
            tv_keyDown.setText("按钮向下：" + keyCode);
        }else if(keyCode == KeyEvent.KEYCODE_MEDIA_REWIND) {
            tv_keyDown.setText("按钮向左：" + keyCode);
        }else if(keyCode == KeyEvent.KEYCODE_FORWARD) {
            tv_keyDown.setText("按钮向右：" + keyCode);
        }else if(keyCode == KeyEvent.KEYCODE_BACK) {
            tv_keyDown.setText("按钮返回：" + keyCode);
        } else if(keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
            tv_keyDown.setText("按钮音量加：" + keyCode);
        }else if(keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
            tv_keyDown.setText("按钮音量减：" + keyCode);
        }else if(keyCode == KeyEvent.KEYCODE_CAMERA) {
            tv_keyDown.setText("按钮摄像头：" + keyCode);
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onStop() {
        Log.i("key", "按钮onStop");
        super.onStop();
    }

    private ScreenStatusReceiver mScreenStatusReceiver;

    private void registSreenStatusReceiver() {
        mScreenStatusReceiver = new ScreenStatusReceiver();
        IntentFilter screenStatusIF = new IntentFilter();
        screenStatusIF.addAction(Intent.ACTION_SCREEN_ON);
        screenStatusIF.addAction(Intent.ACTION_SCREEN_OFF);
        registerReceiver(mScreenStatusReceiver, screenStatusIF);
    }

    class ScreenStatusReceiver extends BroadcastReceiver {
        String SCREEN_ON = "android.intent.action.SCREEN_ON";
        String SCREEN_OFF = "android.intent.action.SCREEN_OFF";

        @Override
        public void onReceive(Context context, Intent intent) {
            if (SCREEN_ON.equals(intent.getAction())) {
                Log.e("key", "屏幕：SCREEN_ON");
            } else if (SCREEN_OFF.equals(intent.getAction())) {
                Log.e("key", "屏幕：SCREEN_OFF");
            }
        }
    }

    @Override
    protected void onDestroy() {
        if(mScreenStatusReceiver != null) {
            unregisterReceiver(mScreenStatusReceiver);
        }

        super.onDestroy();
    }
}
